<?php
namespace app\admin\service;

use think\Loader;
use app\admin\service\Common;
use app\admin\model\Stock AS StockModel;
use app\admin\model\FinanceProfit AS FinanceProfitModel;

class FinanceProfit extends Common {

    const PROFIT_FORECASST = 'https://datacenter.eastmoney.com/securities/api/data/get?type=RPT_F10_FINANCE_GINCOME&sty=APP_F10_GINCOME&filter=';
    const PROFIT_FORECASST_END = "&p=1&ps=5&sr=-1&st=REPORT_DATE&source=HSF10&client=PC&v=";
    public function _initialize(){
    }

    public static function Collect($stockId = 0, $arr = []){
        $year = intval(date("Y"));
        $month = intval(date("m"));
        $day = intval(date("d"));
        $dataTime = '';
        if($month <= 12 && $month >= 9){
            $dataTime = $year."-06";
        }else if($month <= 9 && $month >= 6){
            $dataTime = $year."-03";
        }else if($month <= 3){
            $dataTime = ($year-1)."-12";
        }
        $dataTime .= "-30";
        $url = self::PROFIT_FORECASST.'('.urlencode('SECUCODE="'.$arr['stock_code'].'.'.$arr['jys'].'"').')';
        $url .= "(REPORT_DATE%20in%20(".urlencode("'".$dataTime."'").'))'.self::PROFIT_FORECASST_END;
        $reFinanceProfitJson = "";
        $reFinanceProfitJson = file_get_contents($url);
        if(!empty($reFinanceProfitJson)){
            $reFinanceProfitJson = json_decode($reFinanceProfitJson,true);
            if(!empty($reFinanceProfitJson['result']['data'][0])){
                $reFinanceProfitJson = $reFinanceProfitJson['result']['data'][0];


                $data = [];
                //营业总收入
                $data['gross_operating_income'] = 0;
                if(!empty($reFinanceProfitJson['TOTAL_OPERATE_INCOME'])){
                    $data['gross_operating_income'] = $reFinanceProfitJson['TOTAL_OPERATE_INCOME'];
                }
                //营业收入
                $data['operating_income'] = 0;
                if(!empty($reFinanceProfitJson['OPERATE_INCOME'])){
                    $data['operating_income'] = $reFinanceProfitJson['OPERATE_INCOME'];
                }
                //营业总成本
                $data['total_operat_cost'] = 0;
                if(!empty($reFinanceProfitJson['TOTAL_OPERATE_COST'])){
                    $data['total_operat_cost'] = $reFinanceProfitJson['TOTAL_OPERATE_COST'];
                }
                //营业成本
                $data['operat_cost'] = 0;
                if(!empty($reFinanceProfitJson['OPERATE_COST'])){
                    $data['operat_cost'] = $reFinanceProfitJson['OPERATE_COST'];
                }
                //研发费用
                $data['research_development_expense'] = 0;
                if(!empty($reFinanceProfitJson['RESEARCH_EXPENSE'])){
                    $data['research_development_expense'] = $reFinanceProfitJson['RESEARCH_EXPENSE'];
                }
                //营业税金
                $data['business_tax'] = 0;
                if(!empty($reFinanceProfitJson['OPERATE_TAX_ADD'])){
                    $data['business_tax'] = $reFinanceProfitJson['OPERATE_TAX_ADD'];
                }
                //销售费用
                $data['sell_expense'] = 0;
                if(!empty($reFinanceProfitJson['SALE_EXPENSE'])){
                    $data['sell_expense'] = $reFinanceProfitJson['SALE_EXPENSE'];
                }
                //管理费用
                $data['administrative_expense'] = 0;
                if(!empty($reFinanceProfitJson['MANAGE_EXPENSE'])){
                    $data['administrative_expense'] = $reFinanceProfitJson['MANAGE_EXPENSE'];
                }
                //财务费用
                $data['financial_expense'] = 0;
                if(!empty($reFinanceProfitJson['FINANCE_EXPENSE'])){
                    $data['financial_expense'] = $reFinanceProfitJson['FINANCE_EXPENSE'];
                }
                //利息费用
                $data['interest_expense'] = 0;
                if(!empty($reFinanceProfitJson['FE_INTEREST_EXPENSE'])){
                    $data['interest_expense'] = $reFinanceProfitJson['FE_INTEREST_EXPENSE'];
                }
                //利息收入
                $data['interest_income'] = 0;
                if(!empty($reFinanceProfitJson['FE_INTEREST_INCOME'])){
                    $data['interest_income'] = $reFinanceProfitJson['FE_INTEREST_INCOME'];
                }
                //公允价值变动收益
                $data['gain_changes_fair_value'] = 0;
                if(!empty($reFinanceProfitJson['FAIRVALUE_CHANGE_INCOME'])){
                    $data['gain_changes_fair_value'] = $reFinanceProfitJson['FAIRVALUE_CHANGE_INCOME'];
                }
                //投资收益
                $data['investment_income'] = 0;
                if(!empty($reFinanceProfitJson['INVEST_INCOME'])){
                    $data['investment_income'] = $reFinanceProfitJson['INVEST_INCOME'];
                }
                //对联营企业和合营企业的投资收益
                $data['income_investment_associates_joint_venture'] = 0;
                if(!empty($reFinanceProfitJson['INVEST_JOINT_INCOME'])){
                    $data['income_investment_associates_joint_venture'] = $reFinanceProfitJson['INVEST_JOINT_INCOME'];
                }
                //资产处置收益
                $data['gain_disposal_asset'] = 0;
                if(!empty($reFinanceProfitJson['ASSET_DISPOSAL_INCOME'])){
                    $data['gain_disposal_asset'] = $reFinanceProfitJson['ASSET_DISPOSAL_INCOME'];
                }
                //资产减值损失
                $data['asset_impairment_loss'] = 0;
                if(!empty($reFinanceProfitJson['ASSET_IMPAIRMENT_INCOME'])){
                    $data['asset_impairment_loss'] = $reFinanceProfitJson['ASSET_IMPAIRMENT_INCOME'];
                }
                //信用减值损失
                $data['credit_impairment_loss'] = 0;
                if(!empty($reFinanceProfitJson['CREDIT_IMPAIRMENT_INCOME'])){
                    $data['credit_impairment_loss'] = $reFinanceProfitJson['CREDIT_IMPAIRMENT_INCOME'];
                }
                //其他收益
                $data['other_income'] = 0;
                if(!empty($reFinanceProfitJson['OTHER_INCOME'])){
                    $data['other_income'] = $reFinanceProfitJson['OTHER_INCOME'];
                }
                //营业利润
                $data['operat_profit'] = 0;
                if(!empty($reFinanceProfitJson['OPERATE_PROFIT'])){
                    $data['operat_profit'] = $reFinanceProfitJson['OPERATE_PROFIT'];
                }
                //营业外收入
                $data['non_operat_income'] = 0;
                if(!empty($reFinanceProfitJson['NONBUSINESS_INCOME'])){
                    $data['non_operat_income'] = $reFinanceProfitJson['NONBUSINESS_INCOME'];
                }
                //营业外支出
                $data['non_operat_expense'] = 0;
                if(!empty($reFinanceProfitJson['NONBUSINESS_EXPENSE'])){
                    $data['non_operat_expense'] = $reFinanceProfitJson['NONBUSINESS_EXPENSE'];
                }
                //利润总额
                $data['gross_profit'] = 0;
                if(!empty($reFinanceProfitJson['TOTAL_PROFIT'])){
                    $data['gross_profit'] = $reFinanceProfitJson['TOTAL_PROFIT'];
                }
                //所得税
                $data['income_tax'] = 0;
                if(!empty($reFinanceProfitJson['INCOME_TAX'])){
                    $data['income_tax'] = $reFinanceProfitJson['INCOME_TAX'];
                }
                //持续经营净利润
                $data['net_profit_from_continu_operation'] = 0;
                if(!empty($reFinanceProfitJson['CONTINUED_NETPROFIT'])){
                    $data['net_profit_from_continu_operation'] = $reFinanceProfitJson['CONTINUED_NETPROFIT'];
                }
                //归属于母公司股东的净利润
                $data['net_profit_attributable_shareholders_parent_company'] = 0;
                if(!empty($reFinanceProfitJson['PARENT_NETPROFIT'])){
                    $data['net_profit_attributable_shareholders_parent_company'] = $reFinanceProfitJson['PARENT_NETPROFIT'];
                }
                //少数股东损益
                $data['minority_shareholder_gains_losses'] = 0;
                if(!empty($reFinanceProfitJson['MINORITY_INTEREST'])){
                    $data['minority_shareholder_gains_losses'] = $reFinanceProfitJson['MINORITY_INTEREST'];
                }
                //扣除非经常性损益后的净利润
                $data['net_profit_after_deducting_non_recurr_gains_losses'] = 0;
                if(!empty($reFinanceProfitJson['PARENT_NETPROFIT'])){
                    $data['net_profit_after_deducting_non_recurr_gains_losses'] = $reFinanceProfitJson['PARENT_NETPROFIT'];
                }
                //基本每股收益
                $data['basic_earn_per_share'] = 0;
                if(!empty($reFinanceProfitJson['BASIC_EPS'])){
                    $data['basic_earn_per_share'] = $reFinanceProfitJson['BASIC_EPS'];
                }
                //稀释每股收益
                $data['diluted_earn_per_share'] = 0;
                if(!empty($reFinanceProfitJson['DILUTED_EPS'])){
                    $data['diluted_earn_per_share'] = $reFinanceProfitJson['DILUTED_EPS'];
                }
                //其他综合收益
                $data['other_comprehensive_income'] = 0;
                if(!empty($reFinanceProfitJson['OTHER_COMPRE_INCOME'])){
                    $data['other_comprehensive_income'] = $reFinanceProfitJson['OTHER_COMPRE_INCOME'];
                }
                //归属于母公司股东的其他综合收益
                $data['other_comprehensive_income_attributable_shareholder_parent'] = 0;
                if(!empty($reFinanceProfitJson['PARENT_OCI'])){
                    $data['other_comprehensive_income_attributable_shareholder_parent'] = $reFinanceProfitJson['PARENT_OCI'];
                }      
                //综合收益总额
                $data['total_comprehensive_income'] = 0;
                if(!empty($reFinanceProfitJson['TOTAL_COMPRE_INCOME'])){
                    $data['total_comprehensive_income'] = $reFinanceProfitJson['TOTAL_COMPRE_INCOME'];
                }             
                //归属于母公司股东的综合收益总额
                $data['total_comprehensive_income_attributable_shareholder_parent'] = 0;
                if(!empty($reFinanceProfitJson['PARENT_TCI'])){
                    $data['total_comprehensive_income_attributable_shareholder_parent'] = $reFinanceProfitJson['PARENT_TCI'];
                }
                //归属于少数股东的综合收益总额
                $data['total_comprehensive_income_attributable_minority_shareholder'] = 0;
                if(!empty($reFinanceProfitJson['MINORITY_TCI'])){
                    $data['total_comprehensive_income_attributable_minority_shareholder'] = $reFinanceProfitJson['MINORITY_TCI'];
                }
                $data['remark'] = json_encode($reFinanceProfitJson);
                $where = [];
                $where = ['sid'=>$stockId];
                if(FinanceProfitModel::Detail($where)){
                    FinanceProfitModel::where($where)->update($data);
                }else{
                    $data['sid'] = $stockId;
                    FinanceProfitModel::insert($data);
                }
            }
        }
        return 1;
    }
}